/************************************************************************
Notes:      The final products are a day-period data set (N=357) 
			with following columns:
			* date: date
			* commodity: unique commodity id
			* compliance_period: compliance period number (1-10)
			* commodity_period: commodity period number (1-10)
			* trueup_period: trueup period number (0 if not between a trueup period)
			* compliance_start: start date of a compliance period
			* compliance_end: end date of a compliance period
			* compliance_length: length of a compliance period
			* commodity_start: start date of a commodity period
			* commodity_end: end date of a commodity period
			* commodity_length: length of a commodity period
			* complinace_month_cap: cap (per 30 days)
			* compliance_cap: complinace_month_cap / 30 * compliance_length
			* trade_length: days between compliance_start and commodity_end
			
			and a period data set (N=10) with following columns:
			* period: period number
			* period_start: start date of the compliance period 
			* period_end: end date of the commodity period
			* period_length: number of days between period_start and period_end
			* compliance_end: end date of the compliance period
			* compliance_length: number of days between period_start and compliance_end
*************************************************************************/

********************************************************************************
*** Generate Date-Period Data 
********************************************************************************

set more off
clear all
pause on

local start_date = date("2019/07/16", "YMD")
local end_date = date("2021/03/31", "YMD")
set obs `=`end_date'-`start_date'+1'
egen date = seq(), from(`start_date') to(`end_date')
format %td date
format date %tddd-Mon-CCYY

merge 1:m date using "$TRADING_DATA_OUT/record_plant-commodity-date-bid.dta"
keep date commodity commodity_period
duplicates drop
keep if !missing(date)
sort date

********************************************************************************
** Define Commodity Period 
********************************************************************************

preserve
bysort commodity: egen date_min = min(date)
bysort commodity: egen date_max = max(date)
format date_min %td
format date_max %td
keep commodity commodity_period date_min date_max
duplicates drop
drop if missing(commodity)
reshape long date, i(commodity) j(range) string
sort date
drop range
xtset commodity_period date, delta(1 day)
tsfill
bysort commodity_period: replace commodity = commodity[1]
tempfile commodity_df
save `commodity_df'
restore

drop commodity commodity_period
duplicates drop
merge 1:m date using `commodity_df'
drop _merge
drop if date > td(08apr2021)

* Mock
drop if date < td(16sep2019)
* Interregnum-I
drop if date > td(26mar2020) & date < td(08oct2020) 
* Interregnum-II
drop if date > td(09oct2020) & date < td(01dec2020)

bysort commodity: egen commodity_start = min(date)
bysort commodity: egen commodity_end = max(date)
replace commodity_start = . if missing(commodity)
replace commodity_end = . if missing(commodity)
format commodity_start %tddd-Mon-CCYY
format commodity_end %tddd-Mon-CCYY
sort date commodity_period

* 357 (day, commodity) observations from 16-Sep-2019 to 8-Apr-2021 
* (excluding interregnums)

********************************************************************************
** Define Compliance Period 
********************************************************************************

gen compliance_start = 0
gen compliance_end = 0
gen compliance_period = 0
gen compliance_month_cap = 0
format compliance_start %tddd-Mon-CCYY
format compliance_end %tddd-Mon-CCYY

** Mock-I
local date_start = td(15jul2019)
local date_end = td(13aug2019) 
replace compliance_start = `date_start' if date >= `date_start' 
replace compliance_end = `date_end' - 1 if date >= `date_start' 
replace compliance_month_cap = 280000 if date >= `date_start' 
** Mock-II
local date_start = `date_end'
local date_end = td(16sep2019)
replace compliance_start = `date_start' if date >= `date_start' 
replace compliance_end = `date_end' - 1 if date >= `date_start' 
** Comp-I
local date_start = `date_end'
local date_end = td(16oct2019)
replace compliance_start = `date_start' if date >= `date_start' 
replace compliance_end = `date_end' - 1 if date >= `date_start' 
replace compliance_period = 1 if date >= `date_start' 
** Comp-II
local date_start = `date_end'
local date_end = td(16nov2019)
replace compliance_start = `date_start' if date >= `date_start' 
replace compliance_end = `date_end' - 1 if date >= `date_start' 
replace compliance_period = 2 if date >= `date_start' 
replace compliance_month_cap = 200000 if date >= `date_start' 
** Comp-III
local date_start = `date_end'
local date_end = td(01jan2020)
replace compliance_start = `date_start' if date >= `date_start' 
replace compliance_end = `date_end' - 1 if date >= `date_start' 
replace compliance_period = 3 if date >= `date_start' 
replace compliance_month_cap = 180000 if date >= `date_start' 
** Comp-IV
local date_start = `date_end'
local date_end = td(01feb2020)
replace compliance_start = `date_start' if date >= `date_start' 
replace compliance_end = `date_end' - 1 if date >= `date_start' 
replace compliance_period = 4 if date >= `date_start' 
replace compliance_month_cap = 170000 if date >= `date_start' 
** Comp-V
local date_start = `date_end'
local date_end = td(01mar2020)
replace compliance_start = `date_start' if date >= `date_start' 
replace compliance_end = `date_end' - 1 if date >= `date_start' 
replace compliance_period = 5 if date >= `date_start'
** Comp-VI
local date_start = `date_end'
local date_end = td(22mar2020)
replace compliance_start = `date_start' if date >= `date_start' 
replace compliance_end = `date_end' - 1 if date >= `date_start' 
replace compliance_period = 6 if date >= `date_start' 
** Interregnum-I
local date_start = `date_end'
local date_end = td(12oct2020)
replace compliance_start = `date_start' if date >= `date_start' 
replace compliance_end = `date_end' - 1 if date >= `date_start' 
replace compliance_period = 0 if date >= `date_start' 
** Mock-III
local date_start = `date_end'
local date_end = td(12nov2020)
replace compliance_start = `date_start' if date >= `date_start' 
replace compliance_end = `date_end' - 1 if date >= `date_start' 
replace compliance_period = 0 if date >= `date_start' 
** Interregnum-II
local date_start = `date_end'
local date_end = td(01dec2020)
replace compliance_start = `date_start' if date >= `date_start' 
replace compliance_end = `date_end' - 1 if date >= `date_start' 
replace compliance_period = 0 if date >= `date_start' 
** Comp-VII
local date_start = `date_end'
local date_end = td(01jan2021)
replace compliance_start = `date_start' if date >= `date_start' 
replace compliance_end = `date_end' - 1 if date >= `date_start' 
replace compliance_period = 7 if date >= `date_start' 
** Comp-VIII
local date_start = `date_end'
local date_end = td(01feb2021)
replace compliance_start = `date_start' if date >= `date_start' 
replace compliance_end = `date_end' - 1 if date >= `date_start' 
replace compliance_period = 8 if date >= `date_start' 
** Comp-IX
local date_start = `date_end'
local date_end = td(01mar2021)
replace compliance_start = `date_start' if date >= `date_start' 
replace compliance_end = `date_end' - 1 if date >= `date_start' 
replace compliance_period = 9 if date >= `date_start' 
** Comp-X
local date_start = `date_end'
local date_end = td(01apr2021)
replace compliance_start = `date_start' if date >= `date_start' 
replace compliance_end = `date_end' - 1 if date >= `date_start' 
replace compliance_period = 10 if date >= `date_start' 
** Comp-XI
local date_start = `date_end'
local date_end = td(01may2021)
replace compliance_start = `date_start' if date >= `date_start' 
replace compliance_end = `date_end' - 1 if date >= `date_start' 
replace compliance_period = 11 if date >= `date_start' 

gen compliance_length = (compliance_end - compliance_start) + 1
gen compliance_cap = compliance_month_cap * compliance_length / 30

drop if compliance_period == 0 & missing(commodity_period)
sort date commodity

* 357 (day, commodity) observations from 16-Sep-2019 to 8-Apr-2021 
* (excluding interregnums)

********************************************************************************
** Define True-up Period 
********************************************************************************

gen trueup_period = 0
replace trueup_period = commodity_period if commodity_period + 1 == compliance_period
replace trueup_period = 6 if commodity_period == 6 & compliance_period == 0

gen trade_length = 0
forval i = 1/10 {
	
	preserve
	keep if compliance_period == `i' | trueup_period == `i'
	keep date 
	duplicates drop
	sum date
	local trade_days = r(N)
	restore 

	replace trade_length = `trade_days' if commodity_period == `i'
}

order date commodity compliance_period commodity_period trueup_period compliance_start compliance_end compliance_length compliance_month_cap compliance_cap commodity_start commodity_end trade_length


label var date "Date"
label var commodity "Commodity ID"
label var compliance_period "Compliance period number"
label var commodity_period "Commodity period number"
label var trueup_period "True-up period number (0 if not in True-up period)"
label var compliance_start "Compliance period start date"
label var compliance_end "Compliance period end date"
label var compliance_length "Length of compliance period (days)"
label var compliance_month_cap "Complinace period monthly cap (kg / 30 days)"
label var compliance_cap "Compliance period actual cap = (compliance_month_cap / 30) * compliance_length"
label var commodity_start "First date of trading record in the commodity period"
label var commodity_end "Last date of trading record in the commodity period"
label var trade_length "Compliance + true-up period length"

* 357 (date, period) observations from 16-Sep-2019 to 8-Apr-2021.
save "$TRADING_DATA_OUT/index_day-commodity-period.dta", replace


preserve
keep date commodity commodity_period compliance_start compliance_end commodity_end
rename commodity_period period
rename compliance_start period_start
rename compliance_end period_end_compliance
rename commodity_end period_end_commodity
replace commodity = "PSUM160919" if date == td(16sep2019)
replace period = 1 if date == td(16sep2019)
replace period_end_commodity = td(22oct2019) if date == td(16sep2019)
cross using "$TRADING_DATA_OUT/index_plant.dta"
order gpcb_id
save "$TRADING_DATA_OUT/index_plant-date-period.dta", replace
* 156 plants x 357 (date, period) = 55,692 (plant, date, period) obs.
restore

********************************************************************************
** Generate Period Data
********************************************************************************

keep if compliance_period == commodity_period
rename compliance_period period
gen period_start = compliance_start
format period_start %tddd-Mon-CCYY
label var period_start "Compliance period start date"
rename compliance_end period_end_compliance
rename commodity_end period_end_commodity
rename trade_length period_length 

keep period period_start period_end_compliance period_length period_end_commodity compliance_length compliance_month_cap compliance_cap
order period period_start period_end_compliance period_end_commodity period_length compliance_length compliance_month_cap compliance_cap
duplicates drop
save "$TRADING_DATA_OUT/index_period.dta", replace

********************************************************************************
** Add Bid Day Variable to Trading Record
********************************************************************************

use "$TRADING_DATA_OUT/record_plant-commodity-date-bid.dta", replace

*** create bid day variable
rename commodity_period period
merge m:1 period using "$TRADING_DATA_OUT/index_period.dta", keepusing(period_start period_length period_end_commodity)
drop _merge
gen bid_day = date - period_start + 1
replace bid_day = period_length - (period_end_commodity - date) if bid_day > period_length
gen bid_day_norm = bid_day / period_length
rename period commodity_period
drop period_start period_end_commodity period_length

order bid_day bid_day_norm, after(bid_status)
label var bid_day "Number of day when the bid is placed in a period"
label var bid_day_norm "Bid day normalized = bid day / period length"
sort gpcb_id commodity_period date bid_id

save "$TRADING_DATA_OUT/record_plant-commodity-date-bid.dta", replace
